//
//  personInforViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/27.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "personInforViewController.h"
#import "personInforHeaderView.h"
#import "personInforListCell.h"
#import "personInforEvaluateCell.h"
#import "MBProgressHUD+MJ.h"
@interface personInforViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
/** HeaderView*/
@property (nonatomic, strong) personInforHeaderView *headerView;
/** 样式一的图片数组*/
@property (nonatomic, strong) NSArray *arrPic1;
/** 样式一的名字数组*/
@property (nonatomic, strong) NSArray *arrName;

// 确定点击事件的按钮
@property (nonatomic, assign) BOOL oneOrTwo;
@end

@implementation personInforViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    UIImage *image = [UIImage imageNamed:@"transparent-1"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}
static NSString *const cellListId = @"cellInforListId";
static NSString *const cellEvaluteId= @"personInforEvaluedCellId";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = @"个人信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 设置数据
    self.arrPic1 = @[@"me_personInfor_name",@"me_personInfor_man",@"me_personInfor_iphone",@"me_personInfor_place",@"me_personInfor_school",@"me_personInfor_twoCode",@"me_personInfor_fixPassword"];
    self.arrName = @[@"昵称",@"性别",@"电话",@"学校",@"校区",@"我的二维码",@"修改密码"];
    
    // 设置导航栏
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_back" highImage:@"arrow_back" target:self action:@selector(leftBtnClick)];
    
    // 设置tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, GYMScreenW, GYMScreenH)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    // 注册样式1
    [self.tableView registerNib:[UINib nibWithNibName:@"personInforListCell" bundle:nil] forCellReuseIdentifier:cellListId];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"personInforEvaluateCell" bundle:nil] forCellReuseIdentifier:cellEvaluteId];
    personInforHeaderView *header = [[[NSBundle mainBundle]loadNibNamed:@"personInforHeaderView" owner:nil options:nil]lastObject];
    header.height = 250;
    self.tableView.tableHeaderView = header;
    // 给header增加事件
    [header.btnBack addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [header.btnList addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [header.btnEvaluate addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.headerView = header;
    
    [self.view addSubview:self.tableView];
}

- (void)headerBtnClick:(UIButton *)button{
    
//    [MBProgressHUD showMessage:@"正在刷新"];
    if (button.tag == 2) {
        self.oneOrTwo = YES;
        
        self.headerView.leadingc.constant = GYMScreenW / 2;
        [self.headerView.btnList setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [self.headerView.btnEvaluate setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            if (finished) {
                [MBProgressHUD showSuccess:@"刷新成功"];
                [self.tableView reloadData];
            }
        }];
    }else{
        
        self.oneOrTwo = NO;
        
        self.headerView.leadingc.constant = 0;
        
        [self.headerView.btnList setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.headerView.btnEvaluate setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            if (finished) {
                [MBProgressHUD showSuccess:@"刷新成功"];
                
                [self.tableView reloadData];
            }
        }];
        
    }
}

- (void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITableViewDelegate
// 返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrName.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.oneOrTwo) {
        personInforEvaluateCell *cell = [tableView dequeueReusableCellWithIdentifier:cellEvaluteId];
        cell.textLabel.text = [NSString stringWithFormat:@"历史评价 --- %zd",indexPath.row];
        return cell;
    }else{
        personInforListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellListId];
        [cell.btnName setImage:[UIImage imageNamed:self.arrPic1[indexPath.row]] forState:UIControlStateNormal];
        [cell.btnName setTitle:self.arrName[indexPath.row] forState:UIControlStateNormal];
            return cell;
    }
}


@end
